﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SewerAPI.Core
{
    /// <summary>
    /// Class to hold a collection of statistics. Implements ICollection
    /// to make it queryable.
    /// </summary>
    public class StatisticsCollection : ICollection<Statistic>
    {
        #region Private Fields

        List<Statistic> statistics = new List<Statistic>();

        #endregion

        /// <summary>
        /// Indexer
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Statistic this[int index]{
            get 
            {
                if (index >= 0 && index < statistics.Count)
                {
                    return statistics[index];
                }
                else return null;
            }
            set {
                if (index >= 0 && index < statistics.Count)
                {
                    statistics[index] = value;
                }
                else if (index == statistics.Count)
                {
                    Add(value);
                }
            }
        }

        #region Interface Methods

        public IEnumerator<Statistic> GetEnumerator()
        {
            return statistics.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return statistics.GetEnumerator();
        }

        public void Add(Statistic item)
        {
            statistics.Add(item);
        }

        public void Add(Message message, MessageTransaction transaction, string channel = null)
        {
            Add(Statistic.Create(message, channel));
        }

        public void Clear()
        {
            statistics.Clear();
        }

        public bool Contains(Statistic item)
        {
            return statistics.Contains(item);
        }

        public void CopyTo(Statistic[] array, int arrayIndex)
        {
            statistics.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return statistics.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(Statistic item)
        {
            return false;
        }

        #endregion
    }
}
